import {StringUtils} from "./StringUtils";
import { LStore } from './LStore'

export const ballCamRegex = [
  "r:1",
  "r:2",
  "r:3",
  "r:4",
]

export class UniStore {
  constructor() {
  }

  static setObject(key, object) {
    uni.setStorageSync(key, JSON.stringify(object || {}));
  }

  static getObject(key,type = {}) {
    let json = uni.getStorageSync(key);
    return StringUtils.parseJson(json, type || {});
  }

  static removeObject(key){
    uni.removeStorageSync(key)
  }



  static setItem(key, string) {
    uni.setStorageSync(key, string);
  }

  static getItem(key) {
    let json = uni.getStorageSync(key);
    return json;
  }

  static removeItem(key){
    uni.removeStorageSync(key)
  }



  static setString(key, str) {
    uni.setStorageSync(key, str);
  }

  static getString(key) {
    let str = uni.getStorageSync(key);
    return str
  }

  static setT(token) {
    UniStore.setString('token', token);
  }

  static getT() {
    return UniStore.getString('token');
  }

  static getUserDept(){
    const { depts = [] } = LStore.getUserInfo()
    const [dept] = depts || []
    //F_DepartmentId,F_FullName
    const {F_DepartmentId,F_FullName} = dept || {}
    return {
      dept_name:F_FullName,
      dept_id:F_DepartmentId
    }
  }

  static setUserInfo(UserInfo, loginForm) {
    let {
      F_RealName,
      F_UserId,
      F_Account,
      F_HeadIcon,
      F_Role = [],
      F_Department = [],
      F_Mobile,
      bc_id,
      bc_name
    } = UserInfo;
    let roles = F_Role.map(item=>item['F_EnCode'])
    console.log('用户角色',F_Role,roles)
    let {username,password} = loginForm || {}
    let info = {
      roleCode: roles,
      name: F_RealName,
      id: F_UserId,
      account: F_Account,
      iconSrc: F_HeadIcon,
      mobile:F_Mobile,
      username,
      password,
      bc_id,
      bc_name,
      depts:F_Department
    }

    UniStore.setObject('u_201', info)

  }
  static setTempUserInfo(info = {}){
    let {customId,customName,isTemp,rightList} = info || {}
    if (!isTemp){
      UniStore.removeObject('__u203')
      return
    }

    let camList = []
    rightList.forEach(item => {
      let trimedId = item['item_id']?.trim()
      if (ballCamRegex.includes(trimedId) && !camList.includes(trimedId)) {
        camList.push(item['item_id'])
      }
    })
    console.log(camList)
    UniStore.setObject('__u203', {
      customId,
      customName,
      rightList,
      camList
    })
  }

  static getTempUserInfo(){
    return UniStore.getObject('__u203')
  }
}
